package com.htao.train.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.jwt.Claims;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.htao.train.common.exception.BusinessException;
import com.htao.train.common.exception.BusinessExceptionEnum;
import com.htao.train.common.util.JwtUtil;
import com.htao.train.common.util.SnowUtil;
import com.htao.train.member.domain.Member;
import com.htao.train.member.domain.MemberExample;
import com.htao.train.member.mapper.MemberMapper;
import com.htao.train.member.req.MemberLoginReq;
import com.htao.train.member.req.MemberRegisterReq;
import com.htao.train.member.req.MemberSendCodeReq;
import com.htao.train.member.resp.MemberLoginResp;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author： HTTT
 * @Date： 2023/12/21 21:23
 * @Describe：
 */

@Service
@Slf4j
public class MemberService{
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public int count(){
        return Math.toIntExact(memberMapper.countByExample(null));
    }
    public long register(MemberRegisterReq req){
        String mobile = req.getMobile();
        Member memberDB = getMember(mobile);
        if(ObjectUtil.isNotNull(memberDB)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIT);
        }
        Member member = new Member();
        member.setId(SnowUtil.getSnowflakeNextId());
        member.setMobile(mobile);
        memberMapper.insert(member);
        return member.getId();
    }


    public void sendCode(MemberSendCodeReq req) {
        String mobile = req.getMobile();
        Member memberDB = getMember(mobile);
        if(ObjectUtil.isNull(memberDB)){
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
            log.info("手机号未注册过，已经保存");
        }
        log.info("手机号注册过");
        //生成验证码
     //   String code = RandomUtil.randomString(4);
        //分布式锁 防止频繁发送验证码
/*        String smsSend = stringRedisTemplate.opsForValue().get(mobile);
        if(StrUtil.isNotBlank(smsSend)){
            throw new BusinessException(BusinessExceptionEnum.LOGIN_MUCH);
        }
        stringRedisTemplate.opsForValue().set(mobile,"1",60,TimeUnit.SECONDS);*/
        String code = "8888";
        log.info("验证码为{}",code);
        //保存验证码相关信息
        log.info("保存相关信息");
        //对接短信通道发送短信
        log.info("对接短信通道");

    }

    public MemberLoginResp login(MemberLoginReq req) {
        String mobile = req.getMobile();
        Member memberDB = getMember(mobile);
        if(ObjectUtil.isNull(memberDB)){
            log.info("未发送验证码");
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_CODE);
        }
        String codeReq = req.getCode();
        if(!"8888".equals(codeReq)){
            log.info("验证码错了");
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }
        MemberLoginResp memberLoginResp = BeanUtil.copyProperties(memberDB, MemberLoginResp.class);
        String token = JwtUtil.createToken(memberDB.getId(), memberDB.getMobile());
        memberLoginResp.setToken(token);

        //设置单设备登录
        String key = memberDB.getId()+"_"+memberDB.getMobile()+":";
        Boolean hasKey = stringRedisTemplate.hasKey(key);
        if(hasKey){
            String oldToken = stringRedisTemplate.opsForValue().get(key);
            String redisKey = "blackJwt:" + oldToken;
            stringRedisTemplate.opsForValue().set(redisKey, "1", 24, TimeUnit.HOURS);
        }
        stringRedisTemplate.opsForValue().set(key,token,24, TimeUnit.HOURS);
        return memberLoginResp;
    }

    private Member getMember(String mobile) {
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> members = memberMapper.selectByExample(memberExample);
        if(CollUtil.isEmpty(members)){
            return null;
        }
        return members.get(0);
    }
}
